package com.rd.student.jike.class2.lihongming.Solution713;
import java.util.Arrays;

/**
 * @Auther:Heinrich
 * @Date:2023/7/14-07-14-20:29
 * @Description:com.rd.student.jike.class2.lihongming.Solution713
 * @version:1.0
 */
public class Question11 {
    public static String tictactoe(int[][] board) {
        Character[][] chess = new Character[3][3];
        for (int i = 0; i < board.length; i++) {
            chess[board[i][0]][board[i][1]] = Math.pow(-1, i) == 1 ? 'X' : 'O';
        }
        for (int i = 0; i < chess.length; i++) {
            if(chess[i][0]==chess[i][1]&&chess[i][0]==chess[i][2]){
                if(chess[i][0]=='X'){return "A";}
                else if(chess[i][0]=='O'){
                    return "B";
                }
            }
        }
        for (int i = 0; i < chess[0].length; i++) {
            if(chess[0][i]==chess[1][i]&&chess[0][i]==chess[2][i]){
                if(chess[0][i]=='X'){return "A";}
                else if(chess[0][i]=='O'){
                    return "B";
                }
            }
        }
        if(chess[0][0]==chess[1][1]&&chess[0][0]==chess[2][2]){
            if(chess[0][0]=='X'){return "A";}
            else if(chess[0][0]=='O'){
                return "B";
            }
        }
        if(chess[0][2]==chess[1][1]&&chess[0][2]==chess[2][0]){
            if(chess[0][0]=='X'){return "A";}
            else if(chess[0][0]=='O'){
                return "B";
            }
        }

        int count = 0;
        int[] possibly = new int[2];
        for (int i = 0; i < chess.length; i++) {
            for (int j = 0; j < chess[i].length; j++) {
                if(chess[i][j]!=null){count++;}else{
                    possibly[0]=i;possibly[1]=j;
                }
            }
        }
        if(count==9)return "Draw";
        //用于判断游戏还有一颗子没落得情况下，本局游戏是否还存在胜利者
        if(count==8){
            int[][] newBoard = new int[9][2];
            for (int i = 0; i < 8; i++) {
                newBoard[i] = board[i];
            }
            newBoard[8]=possibly;
            if(tictactoe(newBoard)=="Draw"){
                return "Draw";
            }
        }
        return "Pending";

    }

    public static void main(String[] args) {
        //提前判断本局游戏是否有结果，无结果平局返回draw
        int[][] c = {{0,0},{1,1},{1,0},{2,0},{0,2},{0,1},{2,1},{1,2}};
        //有结果存在胜者返回Pending
        int[][] c1 = {{0,0},{1,1},{1,0},{2,2},{0,2},{0,1},{2,1},{1,2}};
        System.out.println(tictactoe(c));
    }
}
